package malitech.yadam.business.acquire;

import java.io.File;

import malitech.framework.util.TokenReplacer;
import malitech.yadam.model.acquire.CopyJobStepParameters;
import malitech.yadam.model.acquire.JobStepParameters;

public class CopyJobStepRunner implements JobStepRunner
{
    CopyJobStepParameters parameters;
    TokenReplacer tokenReplacer;
    File actualDestinationDirectoryFile;
    @Override
    public void init(JobStepParameters jobStepParameters,TokenReplacer tokenReplacer)
    {
        this.parameters = (CopyJobStepParameters) jobStepParameters;
        this.tokenReplacer = tokenReplacer;
        
        determineActualDestinationDirectoryFullPath();
    }

    @Override
    public void contributeToken()
    {
        tokenReplacer.add(parameters.getActualDestinationDirectoryTokenName()+ ".Name", actualDestinationDirectoryFile.getName());
        tokenReplacer.add(parameters.getActualDestinationDirectoryTokenName()+ ".FullPath", actualDestinationDirectoryFile.getAbsolutePath());
  
    }

    @Override
    public void run()
    {
        // TODO Auto-generated method stub

    }
    
    private void determineActualDestinationDirectoryFullPath()
    {
        // Create file object from 
        File destinationRootFile = new File(parameters.getDestinationRootDirectoryFullPath());
        
        if ( parameters.isMustCreateSubdirectory())
        {
            // Must create an empty sub directory
            String destinationDirectoryName = tokenReplacer.expand(parameters.getSubdirectoryNamePattern());
            actualDestinationDirectoryFile = new File(destinationRootFile,destinationDirectoryName);            
        }
        else
        {
            actualDestinationDirectoryFile = destinationRootFile;
        }
        actualDestinationDirectoryFile.mkdirs();
    }
}
